package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.SortScanTable;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/12
 * 分拣数据操作类
 */
public class SortScanTableDao extends BaseDao<SortScanTable, Long> {
  private static final String TAG = SortScanTableDao.class.getSimpleName();

  public SortScanTableDao() {
    super(DbHelper.getInstance(BaseApplication.getContext()), SortScanTable.class);
  }

  public boolean exist(String waybillNo) {
    try {
      QueryBuilder<SortScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("waybillNo", waybillNo);

      List<SortScanTable> listUnloadScanTable = queryBuilder.query();
      if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
        return true;
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return false;
  }

  public List<SortScanTable> find(String waybillNo) {
    try {
      QueryBuilder<SortScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("waybillNo", waybillNo);

      List<SortScanTable> listUnloadScanTable = queryBuilder.query();
      return listUnloadScanTable;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<SortScanTable> getUnload() {
    try {
      QueryBuilder<SortScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("isUpload", 0);

      List<SortScanTable> listUnloadScanTable = queryBuilder.query();
      return listUnloadScanTable;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<SortScanTable> getUploaded() {
    try {
      QueryBuilder<SortScanTable, Long> queryBuilder = dao.queryBuilder();
      Where where = queryBuilder.where();
      where.eq("isUpload", 1);

      List<SortScanTable> listUnloadScanTable = queryBuilder.query();
      return listUnloadScanTable;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 查询满足条件的数量
   *
   * @param name 字段名
   * @param value 值
   */
  public long queryCount(String name, Object value) {
    long count = 0;
    try {
      QueryBuilder<SortScanTable, Long> queryBuilder = dao.queryBuilder();

      Where where = queryBuilder.where();
      where.eq(name, value);
      count = queryBuilder.countOf();
      return count;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return count;
  }
}
